(function(w){
    var announcement = function(){
        this.Init();
        return this;
    },buttonId = "",c = {},communityId = "",reader = null,contentImgUrl = "",listImgUrl = "",desc = "";

    announcement.prototype = {
        Init: function(){
            if(sessionStorage.loginData != null && sessionStorage.resUrl != null){
                var lData = eval( "(" + sessionStorage.loginData + ")");
                userId = eval( "(" + sessionStorage.loginData + ")").data[0].objId;
                //communityId = "549968e5bc537bb017132f01";
                //userId = "549968e5bc537bb017000a53";
                communityId = lData.data[0].communities[0];
            }else{
                location.href = "/pages/moderatorManager/html/login.html";//没数据进登录页
            }
            c = this;
            $("#announcement_list")[0].style.height = document.body.offsetHeight - $("#announcement_list")[0].offsetTop + "px";

            //读文件
            if(FileReader){
                reader = new FileReader();
            }else{
                alert("不支持FileReader");
            }
            //列表图片上传
            var listImg = document.getElementById("list_img"),contentImg = document.getElementById("content_img");
            listImg.addEventListener("change",function(){
                if(this.files.length == 0)return;
                $(".list-div").remove();
                var formData = new FormData();
                c.readURL(this.files[0]);
                $("#list_img_button").css("display","none");
                formData.append("image1",this.files[0]);
                netWork({"url":"/rest/v1.0/Systems/oldUploadImages","method":"post","formData":formData},c.listImgHandle);

                if(trim($("#announce_title").val()) != "" && trim($("#announce_content").val()) != "") $("#publish").css("color","#F7744A");
            },false);

            //内容图片上传
            contentImg.addEventListener("change",function(){
                if(this.files.length == 0)return;
                $(".content-div").remove();
                var formData = new FormData();
                c.readURL(this.files[0]);
                $("#content_img_button").css("display","none");

                formData.append("image1",this.files[0]);
                netWork({"url":"/rest/v1.0/Systems/oldUploadImages","method":"post","formData":formData},c.contentImgHandle);

            },false);

            c.data = {"url":"/rest/v1.0/backoffice/communities/getAllCommunityNotice/"+communityId};
            netWork(c.data,c.getAnnouncement);
        },
        getAnnouncement: function(data){
            var d = eval( "(" + data + ")"),html = "",date = {};
            $("#announcement_list ul").html("");

            d.data.forEach(function(item){
                date = new Date(item.dateCreated);
                html += "<li data-obj-id = '"+item.objId+"'>"+
                    "<span style = 'display: inline-block;width: 80%;' class = 'announce-details'>"+
                    "<span class = 'announcement-title ell'>"+item.title+"</span><br>"+
                    "<span class = 'announcement-time'>"+formatTime(date)+"</span></span>"+
                    "<span class = 'delete' id = 'delete'>删除</span>"+
                    "<div class = 'wangy' style = 'background: url(/pages/moderatorManager/images/default_img.png) no-repeat center;background-size: 100% auto;'></div>"+
                    "<img src = '"+sessionStorage.resUrl+item.imageURL+"' onload = 'listErrorHandle(this)' style = 'display: none;'>"+
                    "</li>";

                $("#announcement_list ul").html(html);
            });
        },
        deleteHandle: function(data){
            var d = eval( "(" + data + ")");
            c.data = {"url":"/rest/v1.0/backoffice/communities/getAllCommunityNotice/"+communityId};
            netWork(c.data,c.getAnnouncement);
            $("#toast").text("删除成功！");
            $("#toast").addClass("toasts");
        },
        listImgHandle: function(data){
            var d = eval( "(" + data + ")");
            listImgUrl = d.data[0];
        },
        contentImgHandle: function(data){
            var d = eval( "(" + data + ")");
            contentImgUrl = sessionStorage.resUrl + d.data[0];
        },
        readURL: function(imgFile){
            reader.readAsDataURL(imgFile);
            reader.onload = function(){
                var div = document.createElement("div");
                var span = document.createElement("span");
                var img = document.createElement("img");
                img.src = reader.result;
                div.appendChild(img);
                div.appendChild(span);
                if(buttonId == "list_img_button"){
                    img.style.display = "none";
                    span.innerText ="点击更换列表图";
                    div.className = "list-div";
                    div.style.background = "url("+reader.result+") no-repeat center";
                    div.style.backgroundSize = "100% auto";
                    div.id = "list_upload";
                }else{
                    span.innerText = "点击更换内容图";
                    div.className = "content-div";
                    img.id = "list_content";
                }
                document.getElementById("image_result").appendChild(div);
            };
        },
        announcePublish: function(data){
            var d = eval( "(" + data + ")");
            if(d.result.status == 1){
                $("#add_new_announce").css("transform","translate3d(-100%,0,0)");
                $("#add_new_announce").css("-webkit-transform","translate3d(-100%,0,0)");
                c.data = {"url":"/rest/v1.0/backoffice/communities/getAllCommunityNotice/"+communityId};
                netWork(c.data,c.getAnnouncement);
            }else{
                $("#toast").text("发布失败！");
                $("#toast").addClass("toasts");
            }
            $("#list_img").val(null);
            $("#content_img").val(null);
            $("#announce_title").val("");
            $("#announce_content").val("");
            $("#list_img_button").css("display","inline-block");
            $("#content_img_button").css("display","inline-block");
            $("#image_result").html("");
        },
        evt: function(){
            var del = null;
            $("#back").on("click",function(){
                location.href = "/pages/moderatorManager/html/index.html";
            });

            $("#list_img_button").on("click",function(){
                buttonId = this.id;
                var ev = document.createEvent('HTMLEvents');
                ev.initEvent('click', false, true);
                $("#list_img")[0].dispatchEvent(ev);

                //$("#list_img").trigger("click");
            });
            $("#image_result").delegate("#list_upload","click",function(){
                buttonId = "list_img_button";
                $("#list_img").trigger("click");
            });

            $("#content_img_button").on("click",function(){
                buttonId = this.id;
                $("#content_img").trigger("click");
            });
            $("#image_result").delegate("#list_content","click",function(){
                buttonId = "content_img_button";
                $("#content_img").trigger("click");
            });

            $("#add_announce").on("click",function(){
                $("#add_new_announce").css("transform","translate3d(0,0,0)");
                $("#add_new_announce").css("-webkit-transform","translate3d(0,0,0)");
            });

            $("#cancel").on("click",function(){
                $("#add_new_announce").css("transform","translate3d(-100%,0,0)");
                $("#add_new_announce").css("-webkit-transform","translate3d(-100%,0,0)");
            });

            $("#announcement_list").delegate(".announce-details","click",function(){
                location.href = "./announce_details.html?objId="+this.parentNode.dataset.objId;
            });
            $("#announcement_list").delegate("#delete","click",function(){
                del = this;
                tip.confirm({"str":"确定要删除吗？","method":function(){
                    netWork({"url":"/rest/v1.0/backoffice/communities/deleteNoticeByModerator/"+del.parentNode.dataset.objId+"/"+communityId},c.deleteHandle);
                }});
            });

            $("#publish").on("click",function(){
                if(trim($("#announce_title").val()) == ""){
                    $("#toast").text("请输入标题！");
                    $("#toast").addClass("toasts");
                    return;
                }
                if(trim($("#announce_content").val()) == ""){
                    $("#toast").text("请输入内容！");
                    $("#toast").addClass("toasts");
                    return;
                }
                if($("#list_img")[0].files.length == 0){
                    $("#toast").text("请选择列表图！");
                    $("#toast").addClass("toasts");
                    return;
                }

                desc = "<pre style = 'display: block;max-width = 100%;word-wrap: break-word;'>"+$("#announce_content").val()+"</pre><br>"+(contentImgUrl?"<img src = '"+contentImgUrl+"' style = 'width: 100%'>":"");
                netWork({"url":"/rest/v1.0/communities/postCommunityBulletinServiceByModerator/1/1","method":"post","data":"community="+communityId+"&desc="+desc+"&title="+$("#announce_title").val()+"&imageURL="+listImgUrl},c.announcePublish);
            });

            $("#announce_title,#announce_content").on("input",function(){
                if(trim($("#announce_title").val()) == ""){
                    $("#publish").css("color","#F0EAEA");
                    return;
                }
                if(trim($("#announce_content").val()) == ""){
                    $("#publish").css("color","#F0EAEA");
                    return;
                }
                if($("#list_img")[0].files.length == 0) return;

                $("#publish").css("color","#F7744A");
            });

            //提示还原
            $("#toast")[0].addEventListener('webkitAnimationEnd', function(){
                this.className = "toast";
            }, false);
        }
    }
    w.announcement = announcement;
}(window))
new announcement().evt();

var listErrorHandle = function(obj,url){
    setTimeout(function(){
        obj.previousSibling.style.background = "url("+obj.src+") no-repeat center";
        obj.previousSibling.style.backgroundSize = "100% auto";
    });
};
